package com.aoip.telemetry.telemetry.analysis.controller;

import com.aoip.telemetry.telemetry.entity.element.ElementConfigEntity;
import com.aoip.telemetry.telemetry.entity.element.ElementMessageEntity;
import com.aoip.telemetry.telemetry.entity.common.MsgAnalysisItemEntity;
import com.aoip.telemetry.telemetry.entity.common.OriginalMessageEntity;
import com.aoip.telemetry.telemetry.entity.sn.SnHydrologyEntity;
import com.aoip.telemetry.telemetry.analysis.decode.SnHydrologyElementDataDecode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 水文报文解码接口层
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/3/4 15:30
 */
@RestController
public class HydrologyMsgDecodeController {

    @Resource
    private SnHydrologyElementDataDecode snHydrologyMsgDecode;

    /**
     * 水文原始报文解析工具
     *
     * @param msg 原始报文
     * @return java.util.List<com.aoip.telemetry.telemetry.entity.common.MsgAnalysisItemEntity>
     * @author chenbai
     * @date 2022/1/26
     **/
    @GetMapping("/v1/GET/msg/analysis/tool/com.aoip.telemetry.telemetry.hydrology/{msg}")
    public List<MsgAnalysisItemEntity> decode(@PathVariable String msg) {
        int length = msg.length();
        String frameStartCharacter = msg.substring(0, 2);
        String stCode = msg.substring(3, 13);
        String elementData = msg.substring(14, length - 4);
        String crc = msg.substring(length - 4, length - 2);
        String messageEndCharacter = msg.substring(length - 2);

        OriginalMessageEntity originalMsg = new OriginalMessageEntity();
        originalMsg.setMsgBody(elementData);
        originalMsg.setMsgText(msg);
        originalMsg.setStCode(stCode);

        SnHydrologyEntity snHydrologyEntity = new SnHydrologyEntity();
        snHydrologyEntity.setAllMsg(msg);
        snHydrologyEntity.setFrameStartCharacter(frameStartCharacter);
        snHydrologyEntity.setStCode(stCode);
        snHydrologyEntity.setElementData(elementData);
        snHydrologyEntity.setCrc(crc);
        snHydrologyEntity.setMessageEndCharacter(messageEndCharacter);
        Map<String, Object> elementDataDecode = snHydrologyMsgDecode.elementDataDecode(elementData, originalMsg, snHydrologyEntity);

            List<MsgAnalysisItemEntity> resultList = new ArrayList<>();
        if (elementDataDecode != null) {
            resultList.add(new MsgAnalysisItemEntity("帧起始符", snHydrologyEntity.getFrameStartCharacter()));
            resultList.add(new MsgAnalysisItemEntity("遥测站地址", snHydrologyEntity.getStCode()));

            List<ElementMessageEntity> elementList = (List<ElementMessageEntity>) elementDataDecode.get("elementList");
            elementList.forEach(item -> {
                ElementConfigEntity entity = item.getElementConfigEntity();
                StringBuilder sb = new StringBuilder(entity.getIdentifierMsg());
                String identifierValue = entity.getIdentifierValue();
                String fieldUnit = entity.getFieldUnit();
                if (StringUtils.hasText(identifierValue)) {
                    sb.append("(要素标识符：").append(identifierValue).append(")");
                }
                if (StringUtils.hasText(fieldUnit)) {
                    sb.append("(单位：").append(fieldUnit).append(")");
                }
                resultList.add(new MsgAnalysisItemEntity(sb.toString(), item.getValue()));
            });
            resultList.add(new MsgAnalysisItemEntity("校验码", snHydrologyEntity.getCrc()));
            resultList.add(new MsgAnalysisItemEntity("报文结束符", snHydrologyEntity.getMessageEndCharacter()));
        }
        return resultList;
    }
}
